use super::Data;
use leptos::*;
use std::collections::HashMap;

#[allow(non_snake_case)]
#[component]
pub fn Form<F>(
    #[prop(optional)] data_source: HashMap<String, Data>,
    children: Children,
    finish: F,
) -> impl IntoView
where
    F: Fn(HashMap<String, Data>) + 'static,
{
    let (data, set_data) = create_signal(data_source);
    provide_context((data, set_data));
    view! {
        <form class="p-2">
            {children()}
            <button
                class="px-4 py-2 text-xl bg-sky-500 text-white rounded-lg w-full"
                on:click=move |ev| {
                    ev.prevent_default();
                    let v = data();
                    finish(v)
                }
            >
                提交
            </button>
        </form>
    }
}
